package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.entity.AdChannel;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.dtos.ChannelNameDto;
import com.heima.model.admin.dtos.ChannelPageDto;
import jodd.util.CollectionUtil;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-02-01
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 频道分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult pageList(ChannelPageDto dto) {
        //分页查询
        Page<AdChannel> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdChannel> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(dto.getName()),AdChannel::getName,dto.getName());
        lqw.orderByAsc(AdChannel::getOrd);
        page = this.page(page, lqw);
        if (CollectionUtils.isEmpty(page.getRecords())){
            throw new LeadException(AppHttpCodeEnum.AD_USER_NOT_EXIST);
        }
        return new PageResult(page.getPages(),page.getSize(),page.getTotal(),page.getRecords());
    }
    /**
     * 频道新增
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveChannel(ChannelDto dto) {
        if ( dto == null || dto.getName() == null || dto.getStatus() == null){
            return ResponseResult.error(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        AdChannel adChannel = new AdChannel();
        BeanUtils.copyProperties(dto,adChannel);
        adChannel.setCreatedTime(new Date());
        boolean save = this.save(adChannel);
        if (!save){
            return ResponseResult.error(AppHttpCodeEnum.DATA_EXIST);
        }
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult updateChannel(ChannelDto dto) {
        if ( dto == null || dto.getName() == null || dto.getStatus() == null){
            return ResponseResult.error(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        AdChannel adChannel = new AdChannel();
        BeanUtils.copyProperties(dto,adChannel);
        adChannel.setCreatedTime(new Date());
        boolean update = this.updateById(adChannel);
        if (!update){
            return ResponseResult.error(AppHttpCodeEnum.DATA_EXIST);
        }
        return ResponseResult.ok();

    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Long id) {
        if (id == null){
            return ResponseResult.error(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        AdChannel adChannel = this.getById(id);
        if (adChannel.getStatus()){
            throw new LeadException(AppHttpCodeEnum.DATA_CAN_NOT_DEL);
        }
        boolean b = this.removeById(id);
        if (!b){
            return ResponseResult.error(AppHttpCodeEnum.DATA_CAN_NOT_DEL);
        }
        return ResponseResult.ok();
    }

    /**
     * 查询频道id
     * @param id
     * @return
     */
    @Override
    public ChannelNameDto findOne(Integer id) {
        AdChannel adChannel = this.getById(id);
        ChannelNameDto dto = new ChannelNameDto();
        dto.setChannelName(adChannel.getName());
        dto.setId(adChannel.getId());
        return dto;
    }
}
